package com.fzshang.shopping.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.shopping.commen.bean.CommodityClassification;
import com.fzshang.shopping.commen.bean.WheelPlanting;
import com.fzshang.shopping.commen.bean.layuiBean.ResultPage;
import com.fzshang.shopping.commen.bean.layuiBean.VoPage;
import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.entity.ApiResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-03-07
 */
@Controller
@RequestMapping("admin/commodityClassification")
public class CommodityClassificationController extends BaseController {

    @RequestMapping(value = "commodityClassification.html")
    public String commodityClassification(){
        return "admin/commodityClassification/commodityClassification";
    }
    @RequestMapping(value = "commodityClassification_add.html")
    public String commodityClassification_add(){
        return "admin/commodityClassification/commodityClassification_add";
    }
    @RequestMapping(value = "commodityClassification_edit.html")
    public String commodityClassification_edit(Model model,Long id){
        CommodityClassification commodityClassification = commodityClassificationService.getById(id);
        model.addAttribute("commodityClassification",commodityClassification);
        return "admin/commodityClassification/commodityClassification_edit";
    }

    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<CommodityClassification> selectMyPage(VoPage voPage,CommodityClassification commodityClassification){
        return commodityClassificationService.selectMyPage(voPage,commodityClassification);
    }


    /**
     * @param commodityClassification
     * @return 添加和修改
     */
    @RequestMapping(value = "saveOrUpdateById")
    @ResponseBody
    public ApiResult saveOrUpdateById(@RequestBody CommodityClassification commodityClassification){
        commodityClassification.setCreateTime(System.currentTimeMillis());
        commodityClassification.setState(2);
        if(commodityClassificationService.saveOrUpdate(commodityClassification)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }


    /**
     * @param id
     * @return 删除
     */
    @RequestMapping(value = "deleteById")
    @ResponseBody
    public ApiResult deleteById(Long id){
        if(commodityClassificationService.removeById(id)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }


    /**
     * @param id
     * @return 设为首页图标
     */
    @RequestMapping(value = "saveIcon")
    @ResponseBody
    public ApiResult saveIcon(Long id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",1);
        List<CommodityClassification> list = commodityClassificationService.list(queryWrapper);
        if(list.size()>6){
            return ApiResult.fail("最多添加7个图标!");
        }
        CommodityClassification commodityClassification = commodityClassificationService.getById(id);
            commodityClassification.setState(1);
            if(commodityClassificationService.updateById(commodityClassification)){
                return ApiResult.ok();
            }
        return ApiResult.fail();
    }


    /**
     * @param id
     * @return 取消首页图标
     */
    @RequestMapping(value = "set_upIcon")
    @ResponseBody
    public ApiResult set_upIcon(Long id){

        CommodityClassification commodityClassification = commodityClassificationService.getById(id);
            commodityClassification.setState(2);
            if(commodityClassificationService.updateById(commodityClassification)){
                return ApiResult.ok();
            }
        return ApiResult.fail();
    }



}
